#include <stdio.h>
//#include "../include/f3.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#include <time.h>
#define SERVPORT 3333 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
#define MAXDATASIZE 4096 /*每次最大数据传输量 */

typedef struct
{
	char hostname[200];
	long updatetm;
	int pid[500];
	float cpu[500];
	float mem[500];
	float max,min,ave;
}COMPUTER;

COMPUTER host[10];
int num=0;

int float2graph(float in,int x,int y,int my) /*横式*/
{
	in>0.1 && mvaddstr(x,y++,"|");
	float max = COL_MAX;
	int len=(in/max)*(my-y-3),i;
	for(i=0;i<len;i++)
	{
		mvprintw(x,y+i,"|");
	}
	mvaddstr(x,y+i," ");
	mvprintw(x,y+i+1,"%.1f",in);
	return 0;	
}

int float2graph2(float in,int x,int y,float maxin) /*竖式*/
{
	mvaddch(x--,y,ACS_HLINE);
	float max = maxin+5;
	int high = (in/max)*(x-3),i;
	for(i=0;i<high;i++)
	{
		mvaddch(x-i,y,ACS_HLINE);

	}
	mvprintw(x-i,y,"%.1f",in);
	return 0;
}

int InitCOMPUTER(char info[],COMPUTER *in)	/*初始,从文件读入Structure*/
{

	memset(in->cpu,-1,500);
	memset(in->pid,-1,500);
	str2computer(info,in->cpu,in->pid);
	GetMMA(in->cpu,&(in->max),&(in->min),&(in->ave));
//	strcpy(NULL,strtok(NULL,"|||"));
//	strcpy(NULL,strtok(NULL,"|||"));
	return 0;
}
int DrawScr(COMPUTER *in)	/*Stucture输出*/
{
	clear();
	int mx,my,x,y,i,col_len;
	char tmstr[50];
	strcpy(tmstr,ctime(&(in->updatetm)));
	cuthh(tmstr);
	getmaxyx(stdscr,mx,my);
	col_len = (my-6)/COL_MAX;
	x = mx-4;
	y = 3;
	box(stdscr, ACS_VLINE, ACS_HLINE); /*draw a box*/
	for(i=y;i<my-3;i++)
		mvaddch(x,i,ACS_HLINE);
	for(i=0;i<COL_MAX&&in->cpu[i]>1e-6;i++)
	{
		mvprintw(x+1,y+col_len*i,"%5d",in->pid[i]);
		float2graph2(in->cpu[i],x-1,y+col_len*i+2,in->cpu[0]);
	}
	
	attron(A_REVERSE);
	mvprintw(1,my-10-strlen(in->hostname),"Computer:%s",in->hostname);
	mvprintw(2,my-13-strlen(tmstr),"Update time:%s",tmstr);
	mvaddstr(mx-2,3,"Press anykey to Quit...");
	mvprintw(mx-2,my-30,"Max:%4.1f  Min:%4.1f  Ave:%4.1f",in->max,in->min,in->ave);
	attroff(A_REVERSE);
	refresh();
	return 1;
}

void *sub_thread(void *arg)
{
    int *p = (int*)arg,client_fd;
    client_fd = *p;
    int recvbytes=1,host_id;
    host_id=num;
    num++;
    printf("%d\n",*p);
    char buf[4096],tmp[4096];
    while(1)
    {
		recvbytes=recv(client_fd, buf, MAXDATASIZE, 0);
		if(strcmp(buf,"end")==0||recvbytes<1)break;
		strcpy(tmp,strtok(buf,"|||"));
		InitCOMPUTER(tmp,host+host_id);
		strcpy(tmp,strtok(NULL,"|||"));
		strcpy(host[host_id].hostname,tmp);
		strcpy(tmp,strtok(NULL,"|||"));
		str2li(tmp,&host[host_id].updatetm);
		strcpy(buf,"");
    }
	close(client_fd);
	return NULL;
}

void *server_main(void *arg)
{
        int sockfd,client_fd; /*sock_fd：监听socket；client_fd：数据传输socket */
        pthread_t th;
        struct sockaddr_in my_addr; /* 本机地址信息 */
        struct sockaddr_in remote_addr; /* 客户端地址信息 */
        socklen_t sin_size;
        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("socket创建出错！"); exit(1);
        }
        my_addr.sin_family=AF_INET;
        my_addr.sin_port=htons(SERVPORT);
        my_addr.sin_addr.s_addr = INADDR_ANY;
        bzero(&(my_addr.sin_zero),8);
        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
        {
                perror("bind出错！");
                exit(1);
        }
        if (listen(sockfd, BACKLOG) == -1)
        {
                perror("listen出错！");
                exit(1);
        }
        while(1)
        {
                sin_size = sizeof(struct sockaddr_in);
                if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
                {
                        perror("accept出错");
                        continue;
                }
                //printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
                pthread_create(&th,NULL,sub_thread,&client_fd);/*创建子线程用于接受数据*/
        }
	return NULL;
}
int server()
{
    int bo;
    pthread_t th;
    bo = pthread_create(&th,NULL,server_main,NULL);
    if(bo==0)
        return 1;
    return 0;
}
/*
int main()
{
	int pid = fork();
	if(!pid)
	{
		server();
	}
	else
	{
		if(getchar())
			kill(0,15);
	}
	return 0;
}
*/
